package cn.iocoder.yudao.module.book.controller.admin.voice;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.voice.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.voice.VoiceDO;
import cn.iocoder.yudao.module.book.service.voice.VoiceService;

@Tag(name = "管理后台 - 录音")
@RestController
@RequestMapping("/book/voice")
@Validated
public class VoiceController {

    @Resource
    private VoiceService voiceService;

    @PostMapping("/create")
    @Operation(summary = "创建录音")
    @PreAuthorize("@ss.hasPermission('book:voice:create')")
    public CommonResult<Integer> createVoice(@Valid @RequestBody VoiceSaveReqVO createReqVO) {
        return success(voiceService.createVoice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新录音")
    @PreAuthorize("@ss.hasPermission('book:voice:update')")
    public CommonResult<Boolean> updateVoice(@Valid @RequestBody VoiceSaveReqVO updateReqVO) {
        voiceService.updateVoice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除录音")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:voice:delete')")
    public CommonResult<Boolean> deleteVoice(@RequestParam("id") Integer id) {
        voiceService.deleteVoice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得录音")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:voice:query')")
    public CommonResult<VoiceRespVO> getVoice(@RequestParam("id") Integer id) {
        VoiceDO voice = voiceService.getVoice(id);
        return success(BeanUtils.toBean(voice, VoiceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得录音分页")
    @PreAuthorize("@ss.hasPermission('book:voice:query')")
    public CommonResult<PageResult<VoiceRespVO>> getVoicePage(@Valid VoicePageReqVO pageReqVO) {
        PageResult<VoiceDO> pageResult = voiceService.getVoicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VoiceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出录音 Excel")
    @PreAuthorize("@ss.hasPermission('book:voice:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVoiceExcel(@Valid VoicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VoiceDO> list = voiceService.getVoicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "录音.xls", "数据", VoiceRespVO.class,
                        BeanUtils.toBean(list, VoiceRespVO.class));
    }

}